/**
 * Copyright (c) 2010 Moogene
 * All rights reserved
 *
 * This software is the confidential and proprietary information of Moogene.
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into
 * with Moogene
 */
package com.moogene.android.engine.action;

import android.util.Log;
import android.view.MotionEvent;

/**
 * @author Song, jiawei
 *
 */
public class TouchAnalyse {
	private static final String TAG = "TouchAnalyse";
	private static TouchAnalyse sTouchAnalyse;

	private MotionEvent pressMotionEvent;
	private MotionEvent holdMotionEvent;
	private MotionEvent releaseMotionEvent;
	private boolean actionRecord;

	private TouchAnalyse() {

	}

	static TouchAnalyse getInstance() {
		if (sTouchAnalyse == null) {
			sTouchAnalyse = new TouchAnalyse();
		}
		return sTouchAnalyse;
	}


	public boolean onTouch(MotionEvent me) {
		log("onTouch(); inputAct = " + me.getAction());
		actionRecord = false;
		MotionEvent mee = MotionEvent.obtain(me);
		switch (me.getAction()) {
		case MotionEvent.ACTION_DOWN:
			pressMotionEvent = mee;
			actionRecord = true;
			break;
		case MotionEvent.ACTION_CANCEL:
		case MotionEvent.ACTION_UP:
			releaseMotionEvent = mee;
			actionRecord = true;
			break;
		case MotionEvent.ACTION_MOVE:
			holdMotionEvent = mee;
			actionRecord = true;
			break;
		}
		if (actionRecord) {
			return true;
		}
		return false;
	}

	MotionEvent getMotionEvent() {
		MotionEvent me = null;
		if (pressMotionEvent != null) {
			me = MotionEvent.obtain(pressMotionEvent);
			pressMotionEvent = null;
			return me;
		} else if (releaseMotionEvent != null) {
			me = MotionEvent.obtain(releaseMotionEvent);
			pressMotionEvent = null;
			holdMotionEvent = null;
			releaseMotionEvent = null;
			return me;
		} else if (holdMotionEvent != null) {
			me = MotionEvent.obtain(holdMotionEvent);
			pressMotionEvent = null;
		}
		return me;
	}

	static void log(String msg) {
		if (Config.LOG_ENABLE) {
			Log.d(Config.LOG_TAG + "." + TAG, msg);
		}
	}
}
